package me.firstapp.manage.topic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.json.AbstractJsonObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonReplyInManage;
import me.firstapp.common.module.bbs.JsonTopic;
import me.firstapp.common.module.bbs.JsonTopicInManage;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.manage.common.BaseManage;
import me.firstapp.manage.utils.BizData4Page;
import me.firstapp.spi.bbs.ReplyService;
import me.firstapp.spi.bbs.TopicService;

@Controller
@RequestMapping(value = "/topicManage")
public class TopicManageAct extends BaseManage {

	@Autowired
	private TopicService topicService;

	@Autowired
	private ReplyService replyService;

	@ResponseBody
	@RequestMapping(value = "/findTopicPage.do", method = RequestMethod.POST)
	public void findTopicPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			String startTime = null;
			String endTime = null;
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						if ("createTime".equals(key)) {
							// 日期范围判断
							String registerTimes = (String) jsonObject.get(key);
							if (registerTimes.contains(",")) {
								String[] registerTimesArra = registerTimes.split(",");
								startTime = registerTimesArra[0];
								endTime = registerTimesArra[1];
								if ("0".equals(startTime)) {
									startTime = null;
								}
								if ("0".equals(endTime)) {
									endTime = null;
								}
							}
						} else {
							searchParams.put(key, jsonObject.get(key));
						}
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonTopicInManage> topicPage = topicService.findTopicsPageByParams(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0,
						StrUtils.isNULL(startTime) ? null : Long.valueOf(startTime),
						StrUtils.isNULL(endTime) ? null : Long.valueOf(endTime));
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonTopicInManage> bizData4Page = new BizData4Page<JsonTopicInManage>();
				bizData4Page.setUserdata(conditions);
				if (topicPage.getPage() != null) {
					bizData4Page.setPage(topicPage.getPage().getPageNo());
					bizData4Page.setPagesize(topicPage.getPage().getPageSize());
					bizData4Page.setRecords(topicPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonTopicInManage>(topicPage.getPage().getList()));
					bizData4Page.setTotal(topicPage.getPage().getTotalCount());
				}

				result.setCode(topicPage.getCode());
				result.setMsg(topicPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/findReplyPage.do", method = RequestMethod.POST)
	public void findReplyPage(@RequestParam Map<String, Object> conditions, Long topicId, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonReplyInManage> replyPage = replyService.findRepliesPageByParams(topicId, pageNo,
						iDisplayLength, searchParams, mDataProp, sSortDir_0);
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonReplyInManage> bizData4Page = new BizData4Page<JsonReplyInManage>();
				bizData4Page.setUserdata(conditions);
				if (replyPage.getPage() != null) {
					bizData4Page.setPage(replyPage.getPage().getPageNo());
					bizData4Page.setPagesize(replyPage.getPage().getPageSize());
					bizData4Page.setRecords(replyPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonReplyInManage>(replyPage.getPage().getList()));
					bizData4Page.setTotal(replyPage.getPage().getTotalCount());
				}

				result.setCode(replyPage.getCode());
				result.setMsg(replyPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@RequestMapping(value = "/deleteTopics.do", method = RequestMethod.POST)
	public void deleteTopics(String ids, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = topicService.deleteTopics(ids);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteReplies.do", method = RequestMethod.POST)
	public void deleteReplies(String ids, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = replyService.deleteReplies(ids);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateTopic.do", method = RequestMethod.POST)
	public void updateTopic(Long topicId, String summary, String content, Long upCounts, Long viewCounts,
			Long replyCounts, Long collectCounts, Integer recommend, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<JsonTopic> result = topicService.updateTopic(topicId, summary, content, upCounts, viewCounts,
				replyCounts, collectCounts, recommend);
		ResponseUtils.renderJson(response, result);
	}

}
